<script setup lang="ts">
import { ref, onMounted } from "vue";
import { getCurrentWindow } from "@tauri-apps/api/window";
import MainView from "./views/MainView.vue";
import CalendarView from "./views/CalendarView.vue";
import FloatingWindow from "./views/FloatingWindow.vue";
import { useTheme } from "./composables/useTheme";
import { useSettingsStore } from "./stores/settings";
import { useTodoStore } from "./stores/todo";

const settingsStore = useSettingsStore();
const todoStore = useTodoStore();
const currentWindow = getCurrentWindow();
const isFloatingWindow = ref(false);

// 初始化主题系统
useTheme();

onMounted(async () => {
  await settingsStore.loadSettings();

  // 检查当前窗口是否是悬浮窗口
  isFloatingWindow.value = currentWindow.label === "floating";

  // 如果是主窗口，监听焦点事件重新加载数据
  if (!isFloatingWindow.value) {
    await currentWindow.onFocusChanged(async ({ payload: focused }) => {
      if (focused) {
        await todoStore.loadTodos();
      }
    });
  }
});
</script>

<template>
  <FloatingWindow v-if="isFloatingWindow" />
  <MainView v-else-if="settingsStore.settings.viewMode === 'list'" />
  <CalendarView v-else />
</template>
